<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 

        key的使用：

            1. 如果数据有破环顺序的操作时，并且有输入类的元素时，使用 index 做为 key 会有意想不到的快乐。



        { id: "001", name: "apple" },
        { id: "002", name: "banana" },

        虚拟DOM
        <li key="0">apple<input type="text" /></li>
        <li key="1">banana<input type="text" /></li>

        真实DOM
        <li>
            apple
            <input type="text" value="apple" />
        </li>
        <li>
            banana
            <input type="text" value="banana" />
        </li>

        更新数据

        { id: "003", name: "orange" },
        { id: "001", name: "apple" },
        { id: "002", name: "banana" },

        虚拟DOM
        <li key="0"> orange <input type="text" /></li>
        <li key="1"> apple <input type="text" /></li>
        <li key="2"> banana <input type="text" /></li>

        新旧虚拟DOM 比对差异 diff



        { id: "001", name: "apple" },
        { id: "002", name: "banana" },

        虚拟DOM
        <li key="0">apple<input type="text" /></li>
        <li key="1">banana<input type="text" /></li>

        真实DOM
        <li>
            apple
            <input type="text" value="apple" />
        </li>
        <li>
            banana
            <input type="text" value="banana" />
        </li>

        更新数据

        
        { id: "001", name: "apple" },
        { id: "002", name: "banana" },
        { id: "003", name: "orange" },

        虚拟DOM
        
        <li key="0"> apple <input type="text" /></li>
        <li key="1"> banana <input type="text" /></li>
        <li key="2"> orange <input type="text" /></li>

        新旧虚拟DOM 比对差异 diff

        
     -->

    <div id="app">
      <button @click="add">点击 在list的最前面添加一个 orange</button>
      <ul>
        <li v-for="(item, index) in list" :key="index">
          {{ item.name }}
          <input type="text" />
        </li>
      </ul>
    </div>

    <script src="./js/vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        // 根组件的 data 选项也可以使用函数
        // data:
        data: function () {
          return {
            list: [
              { id: "001", name: "apple" },
              { id: "002", name: "banana" },
            ],
          };
        },
        methods: {
          add() {
            this.list.push({
              id: "003",
              name: "orange",
            });
          },
        },
      });
    </script>
  </body>
</html>
